using System;
using System.ServiceProcess;
using System.Timers;

using umlungu.engineX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2007-10-27
	Status:		beta
	Version:	2.0.0
	Build:		20071027
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20071027:	Start of .Net2 version from WinServiceX.0.0.1
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.winserviceX {
	/// <summary>
	/// A base class for a Windows Service that runs on a schedule
	/// <para>Not intended to be used directly, but rather as a base class</para>
	/// <para>Note that the derived class should define the Main() method as the entry point, as follows:
	///		protected void Main() {
	///			ServiceBase.Run(new x_winservice());
	///		}
	/// </para>
	/// </summary>
	public class x_winservice : ServiceBase {
		#region Invisible properties
		private const string DEFAULT_NAME = "WinServiceX";
		private const string DEFAULT_INTERVAL_MINUTES = "5";
		private const string CONFIG_INTERVAL_PATH = "WinServiceX/@IntervalMinutes";
		#endregion

		#region Visible properties
		private Timer timer;
		/// <summary>The timer that runs the service</summary>
		/// <value>a System.Timers.Timer</value>
		public Timer Timer {
			get { return timer; }
		}
		private x_config config;
		/// <summary>The engineX configuration object</summary>
		/// <value>a config object</value>
		public x_config Config {
			get { return config; }
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_winservice() {
			initialise(DEFAULT_NAME);
		}
		/// <summary>Constructor with service name</summary>
		public x_winservice(string name) {
			initialise(name);
		}
		#endregion

		#region Public methods
		/// <summary>
		/// Description of this method
		/// </summary>
		public void WriteLog(string message) {
			EventLog.WriteEntry(String.Concat(ServiceName, ": ", message));
		}
		/// <summary>
		/// Description of this method
		/// </summary>
		protected virtual void RunCommands() {
			/*
			JobRunWebService.JobRun objJob = new JobRunWebService.JobRun();
			objJob.Credentials = System.Net.CredentialCache.DefaultCredentials;
			objJob.RunJob();
			*/
			WriteLog("Service ran ok");
		}
		#endregion
		
		#region Protected methods
		/// <summary>
		/// Gets the timer interval from the config object (can be overridden)
		/// </summary>
		protected virtual double GetTimerInterval() {
			string configInterval = config.Value(CONFIG_INTERVAL_PATH);
			double interval = Convert.ToDouble((configInterval != null) ? configInterval : DEFAULT_INTERVAL_MINUTES) * 60000;
			return interval;
		}
		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		protected override void OnStart(string[] args) {
			WriteLog("Service started");
			try {
				initialiseTimer();
				WriteLog(String.Concat("Timer interval set to: ", timer.Interval.ToString()));
				if (timer != null) {
					timer.Enabled = true;
				}
			} catch(Exception e) {
				WriteLog(String.Concat("Error starting service: ", e.Message, "::", e.StackTrace));
			}
		}
 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop() {
			WriteLog("Service stopped");
			if (timer != null)
				timer.Enabled = false;
		}
		/// <summary>
		/// Pause this service.
		/// </summary>
		protected override void OnPause() {
			WriteLog("Service paused");
			if (timer != null)
				timer.Enabled = false;
		}
		/// <summary>
		/// Continue this service.
		/// </summary>
		protected override void OnContinue() {
			WriteLog("Service continued");
			if (timer != null)
				timer.Enabled = true;
		}
		#endregion
				
		#region Private methods
		/// <summary>
		/// Description of this method
		/// </summary>
		private void initialise(string name) {
			config = new x_config();
			CanPauseAndContinue = true;
			ServiceName = name;
		}
		/// <summary>
		/// Description of this method
		/// </summary>
		private void initialiseTimer() {
			if (timer == null) {
				timer = new Timer();
				timer.AutoReset = true;
				timer.Interval = GetTimerInterval();
				timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
			}
		}
		/// <summary>
		/// Description of this method
		/// </summary>
		private void timer_Elapsed(object source,System.Timers.ElapsedEventArgs e) {
			RunCommands();
		}
		#endregion
	}
}